package com.domor.controller.project;

import com.domor.model.Result;
import com.domor.model.Table;
import com.domor.model.User;
import com.domor.service.project.ProjectService;
import com.domor.utils.CommonUtils;
import com.domor.utils.ParamUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("project")
public class ProjectController {

    @Autowired
    private ProjectService service;

    @RequestMapping("queryProject")
    public Object queryProject(HttpServletRequest request) {
        User user = CommonUtils.getSessionUser(request);
        Map<String, Object> params = ParamUtils.getParams(request);

        int isLeader = user.getIsLeader();
        String roleKey = user.getRoleKey();
        if (isLeader == 1) {
            params.put("team", user.getId());
        } else {
            if (roleKey.equals("PRO")) params.put("pro", user.getId());
            if (roleKey.equals("DES")) params.put("des", user.getId());
            if (roleKey.equals("TEC")) params.put("tec", user.getId());
        }

        Page page = CommonUtils.startPage(params);
        Table table = new Table(service.queryProject(params), page.getTotal());
        return Result.success(table);
    }

    @RequestMapping("queryDemand")
    public Result queryDemand(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        Page page = CommonUtils.startPage(params);
        Table table = new Table(service.queryDemand(params), page.getTotal());
        return Result.success(table);
    }

    @RequestMapping("queryCustomer")
    public Result queryCustomer(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        return Result.success(service.queryCustomer(params));
    }

    @RequestMapping("queryContact")
    public Result queryContact(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        return Result.success(service.queryContact(params));
    }

    @RequestMapping("saveProject")
    public Result saveProject(HttpServletRequest request, int id) {
        Map<String, Object> params = ParamUtils.getParams(request);
        int count = service.getByName(params.get("name").toString());
        if (count > 0) return Result.error("项目名称不允许重复！");
        if (id == 0) {
            service.insertProject(params);
        } else {
            service.updateProject(params);
        }
        return Result.success();
    }

    @RequestMapping("saveDemand")
    public Object saveDemand(HttpServletRequest request,int id) {
        Map<String, Object> params = ParamUtils.getParams(request);
        if (id == 0) {
            service.insertDemand(params);
        } else {
            service.updateDemand(params);
        }
        return Result.success();
    }

}
